<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="el-box">
        <treetools :tree-node="company" :is-root="isRoot" @addDepts="adddepts" />
        <el-tree class="el-top" :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 树形结构 -->
          <treetools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartmentsList()"
            @addDepts="adddepts"
            @edit="editdepar"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 弹窗组件 -->
    <add-dept
      ref="Editor"
      :show-dialog="showDialog"
      :treenode="node"
      @addDepar="getDepartmentsList()"
      @cancel="showDialog = false"
    />
  </div>
</template>

<script>
import treetools from './components/tree-tools'
import addDept from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
export default {
  // 组件名称
  name: '',
  // 局部注册的组件
  components: {
    treetools,
    addDept
  },
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data() {
    return {
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      departs: [],
      defaultProps: {
        children: 'children',
        label: 'name' // 表示 从这个属性显示内容
      },
      isRoot: true,
      showDialog: false, // 弹窗显示
      node: null, // 当前点击的部门
      loading: false
    }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 生命周期
  created() {
    this.getDepartmentsList()
  },
  // 组件方法
  methods: {
    /*
     *怎么判断
     *
     */
    // 获取树形数据
    async getDepartmentsList() {
      this.loading = true
      const result = await getDepartments()
      // 这里加一个空id是为了后面表单校验做处理的
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      console.log(result)
      this.departs = tranListToTreeData(result.depts, '')
      this.loading = false
    },
    // 显示弹窗并将点击的id保存到node
    adddepts(value) {
      this.showDialog = true
      this.node = value
    },
    editdepar(value) {
      this.showDialog = true
      this.node = value
      // console.log(this.$refs.Editor)
      // 获取弹出层的实例
      this.$refs.Editor.getDepartDetail(value.id)
    }
  }
}
</script>

<style scoped>
.el-box {
  padding: 30px 140px;
  font-size: 14px;
}
.el-top {
  margin-top: 20px;
}
</style>
